package tech.mobi.child.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class Tools {

	/**
	 * XML后台参数字符串头
	 * @return
	 */
	public static String getConstParamentHeader() {
		String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<mobile><request>";
		return str;
	}
	/**
	 * XML后台参数字符串尾
	 * @return
	 */
	public static String getConstParamentFooter(String strVersion) {
		String str = "<clientflag>"+Configuration.APP_FLAG+"</clientflag>" 
			+"<clientos>"+Configuration.CLIENTOS+"</clientos>" 
			+"<clientappversion>"+strVersion+"</clientappversion>" 
			+"</request></mobile>";
		return str;
	}
	
	/**
	 * 字符串转整数
	 * @param str
	 * @param defValue
	 * @return
	 */
	public static int toInt(String str, int defValue) {
		try{
			return Integer.parseInt(str);
		}catch(Exception e){}
		return defValue;
	}
	/**
	 * 对象转整数
	 * @param obj
	 * @return 转换异常返回 0
	 */
	public static int toInt(Object obj) {
		if(obj==null) return 0;
		return toInt(obj.toString(),0);
	}
	
	/**
	 * 点击返回监听事件
	 * @param activity
	 * @return
	 */
	public static View.OnClickListener finish(final Activity activity)
	{
		return new View.OnClickListener() {
			public void onClick(View v) {
				activity.finish();
			}
		};
	}	
	/**
	 * 弹出Toast消息
	 * @param msg
	 */
	public static void ToastMessage(Context cont,String msg)
	{
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}
	public static void ToastMessage(Context cont,int msg)
	{
		Toast.makeText(cont, msg, Toast.LENGTH_SHORT).show();
	}
	public static void ToastMessage(Context cont,String msg,int time)
	{
		Toast.makeText(cont, msg, time).show();
	}
	/**
	 * 时间转换字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2String(Date date,String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String dateStr = simpleDateFormat.format(date);
		return dateStr;
	}
	
	public static Date StringToDate(String dateStr,String formatStr){
		SimpleDateFormat dd=new SimpleDateFormat(formatStr);
		Date date=null;
		try {
			date = dd.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 得到版本号
	 */
	public static String getVersionName(Context context) {
		String versionName = "";
		PackageManager packageManager = context.getPackageManager();
		try {
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			versionName = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}
	
	//<meta-data android:name="api_key" android:value="xxx" />
	// 从Manifest.xml配置文件中获取数据
	public static String getMetaValue(Context context, String metaKey) {
		Bundle metaData = null;
		String metaValue = null;
		if (context == null || metaKey == null) {
			return null;
		}
		try {
			ApplicationInfo ai = context.getPackageManager()
					.getApplicationInfo(context.getPackageName(),
							PackageManager.GET_META_DATA);
			if (null != ai) {
				metaData = ai.metaData;
			}
			if (null != metaData) {
				metaValue = metaData.getString(metaKey);
			}
		} catch (NameNotFoundException e) {
		}
		return metaValue;// xxx
	}
	
	/**
	 * 保留小数点后2位（四舍五入），且不按科学计数法输出
	 * @param value
	 * @return
	 */
	public static String GetThousandsString(double value) { 
		// 

		String retValue = null; 

		DecimalFormat df = new DecimalFormat(); 

		df.setMinimumFractionDigits(2); 

		df.setMaximumFractionDigits(2); 

		retValue = df.format(value); 

		//retValue = retValue.replaceAll(",", ""); 

		return retValue; 

	}

}